package org.jsola.hr.helper;

import org.jsola.common.ClassKit;
import org.jsola.common.DateKit;
import org.jsola.common.SafeKit;
import org.jsola.hr.dto.NewsInfoAddDTO;
import org.jsola.hr.dto.NewsInfoUpdateDTO;
import org.jsola.hr.entity.NewsInfoDO;
import org.jsola.hr.query.NewsInfoQuery;
import org.jsola.hr.service.INewsInfoService;
import org.jsola.hr.vo.NewsInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

import static org.jsola.hr.core.AbstractTest.SITE_ID;



/**
 * 官网信息的类型
 *
 * @author zhr
 */
@Component
public class NewsInfoTestHelper extends BaseTestHelper {
    
    @Autowired
    private INewsInfoService newsInfoService;

    /**
     * 生成add测试数据
     * @return NewsInfoAddDTO
     */
    public NewsInfoAddDTO getNewsInfoAddDTO(){
        NewsInfoAddDTO newsInfoAddDTO = new NewsInfoAddDTO();
        newsInfoAddDTO.setBrowseNum(SafeKit.getInteger(getRandomValue("int",0)));
        newsInfoAddDTO.setNewsTypeId(getRandomValue("varchar",32));
        newsInfoAddDTO.setTitle(getRandomValue("text",2147483647));
        newsInfoAddDTO.setContent(getRandomValue("text",2147483647));
        newsInfoAddDTO.setContentHasStyle(getRandomValue("text",2147483647));
        newsInfoAddDTO.setToUrl(getRandomValue("varchar",200));
        newsInfoAddDTO.setIsPush(SafeKit.getBoolean(getRandomValue("bigint",1)));
        newsInfoAddDTO.setPushDate((Date)ClassKit.getObjectValue(getRandomValue("datetime",0),"java.util.Date"));
        newsInfoAddDTO.setPushUserId(getRandomValue("varchar",32));
        newsInfoAddDTO.setPushUserName(getRandomValue("varchar",50));
        return newsInfoAddDTO;
    }


    /**
     * 生成update测试数据
     * @return NewsInfoUpdateDTO
     */
    public NewsInfoUpdateDTO getNewsInfoUpdateDTO(NewsInfoVO newsInfoVO){
        return newsInfoService.selectDOById(newsInfoVO.getId(), SITE_ID).to(NewsInfoUpdateDTO.class);
    }

    /**
     * 生成查询数据
     * @return NewsInfoQuery
     */
    public NewsInfoQuery getNewsInfoQuery() {
        NewsInfoQuery newsInfoQuery = new NewsInfoQuery();
        List<NewsInfoDO> newsInfoDOList = newsInfoService.list(new NewsInfoDO(),SITE_ID);
        if (!CollectionUtils.isEmpty(newsInfoDOList)) {
            NewsInfoDO newsInfoDO = newsInfoDOList.get(0);
            newsInfoQuery = newsInfoDO.to(NewsInfoQuery.class);
            if (newsInfoDO.getPushDate() != null) {
                newsInfoQuery.setPushDateStart(DateKit.getBeforeDay(newsInfoDO.getPushDate(),-1));
                newsInfoQuery.setPushDateEnd(DateKit.getBeforeDay(newsInfoDO.getPushDate(),1));
            }
        }
        return newsInfoQuery;
    }

    /**
     * 保存示例
     * @return NewsInfoVO
     */
    public NewsInfoVO saveNewsInfoExample(){
        NewsInfoAddDTO addDTO = this.getNewsInfoAddDTO();
        NewsInfoVO newsInfoVO = newsInfoService.save(addDTO,buildTokenUser());
        return newsInfoVO;
    }


}

